import applicationApi from '@/api/application'
import UserApi from '@/api/user'

export function asyncGetAppProfile(loading?: boolean) {
  return new Promise((resolve, reject) => {
    applicationApi
      .getAppProfile(loading)
      .then((data) => {
        resolve(data)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export function asyncAppAuthentication(
  token: string,
  loading?: boolean,
  authentication_value?: any
) {
  return new Promise((resolve, reject) => {
    applicationApi
      .postAppAuthentication(token, loading, authentication_value)
      .then((res) => {
        localStorage.setItem('accessToken', res.data)
        sessionStorage.setItem('accessToken', res.data)
        resolve(res)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export function asyncGetProfile() {
    return new Promise((resolve, reject) => {
      UserApi.getProfile()
        .then(async (ok) => {
          window.user.version = ok.data?.version || '-'
          window.user.isXPack = ok.data?.IS_XPACK
          window.user.XPACK_LICENSE_IS_VALID = ok.data?.XPACK_LICENSE_IS_VALID
          // if (window.user.isEnterprise()) {
          //   await window.user.theme()
          // } 
          resolve(ok)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
